package com.pactera.madp.cp.controller.sidebar.daily;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.log.annotation.SysLog;
import com.pactera.madp.common.security.annotation.Inner;
import com.pactera.madp.cp.api.entity.CorpEntity;
import com.pactera.madp.cp.api.util.Day;
import com.pactera.madp.cp.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.List;

/**
 * @description:企业日报管理
 * @author: Huayu
 * @time: 2020/11/23 18:19
 */

@RestController
@AllArgsConstructor
@RequestMapping("/daily")
@Api(value = "daily", tags = "企业日报管理")
public class DailyController {

    private DailyService dailyService;
    private IWorkContactRoomDailyService workContactRoomDailyService;
    private IWorkContactDailyService workContactDailyService;
    private ICorpService corpService;

    @GetMapping("/daily")
    @ApiOperation(value = "数据日报")
    public R daily(String endDate, Integer type, Integer dailyType) {
        if (type == 0) {
            return R.ok(workContactDailyService.dailyQuery(endDate, dailyType));
        } else if (type == 1) {
            return R.ok(workContactRoomDailyService.dailyQuery(endDate, dailyType));
        }
        return R.failed("请选择日报类型");
    }

    @SysLog("日报生成")
    @Inner
    @GetMapping("/generate")
    @ApiOperation(value = "日报生成")
    public void generate() {
        Day day = new Day(Calendar.getInstance());
        List<Integer> ids = corpService.listObjs(Wrappers.<CorpEntity>lambdaQuery().select(CorpEntity::getCorpId), o -> Integer.parseInt(o.toString()));
        ids.forEach(corpId -> {
            if (day.isWeekEnd()) {
                dailyService.generate(corpId, 1, day);
                dailyService.generate(corpId, 2, day);
            } else if (day.isMonthEnd()) {
                dailyService.generate(corpId, 1, day);
                dailyService.generate(corpId, 3, day);
            } else {
                dailyService.generate(corpId, 1, day);
            }
        });
    }
    @GetMapping("/reminder")
    @ApiOperation(value = "日报提醒")
    public R reminder(Integer corpId, Integer type) {
        return dailyService.reminder(corpId, type);
    }

}
